package com.longjianghuasheng.logistics.data.bindadapter

import android.annotation.SuppressLint
import android.os.SystemClock
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import coil.load
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.longjianghuasheng.logistics.network.NetworkApi
import com.longjianghuasheng.logistics.R

import com.xuexiang.xui.utils.CountDownButtonHelper
import com.xuexiang.xui.widget.button.ButtonView
import com.xuexiang.xui.widget.button.SmoothCheckBox
import me.hgj.jetpackmvvm.ext.view.textString

object CustomBindAdapter {


    @BindingAdapter(value = ["startCountDown"])
    @JvmStatic
    fun startCountDown(buttonView: ButtonView, countDownTime: Int) {

        CountDownButtonHelper(buttonView, countDownTime).setOnCountDownListener(object :
            CountDownButtonHelper.OnCountDownListener {
            @SuppressLint("SetTextI18n")
            override fun onCountDown(time: Int) {

                buttonView.text = "${time}S"
            }

            override fun onFinished() {
                buttonView.text = StringUtils.getString(R.string.get_code)

            }
        })
            .start()

    }


    @BindingAdapter(value = ["checked"])
    @JvmStatic
    fun checked(smoothBox: SmoothCheckBox, boolean: Boolean) {
        smoothBox.isChecked = boolean

    }

    @InverseBindingAdapter(attribute = "checked", event = "checkAttrChanged")
    @JvmStatic
    fun getCheck(view: SmoothCheckBox?): Boolean = view?.isChecked ?: false


    @BindingAdapter("checkAttrChanged")
    @JvmStatic
    fun setCompoundCheckListener(view: SmoothCheckBox, checkAttrChanged: InverseBindingListener) {
        view.setOnCheckedChangeListener { buttonView, isChecked ->
            checkAttrChanged.onChange()
        }
    }

    @BindingAdapter(value = ["checkChange"])
    @JvmStatic
    fun checkChange(checkbox: CheckBox, listener: CompoundButton.OnCheckedChangeListener) {
        checkbox.setOnCheckedChangeListener(listener)
    }

    @BindingAdapter(value = ["showPwd"])
    @JvmStatic
    fun showPwd(view: EditText, boolean: Boolean) {
        if (boolean) {
            view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            view.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        view.setSelection(view.textString().length)
    }

    @BindingAdapter(value = ["imageUrl"])
    @JvmStatic
    fun imageUrl(view: ImageView, url: String) {


        view.load("${NetworkApi.BASE_URL}/$url") {
            placeholder(R.drawable.shape_album_loading_bg)
            error(R.drawable.shape_album_loading_bg)
            crossfade(true) //淡入淡出
        }

//        Glide.with(view.context.applicationContext)
//            .load(url)
//            .transition(DrawableTransitionOptions.withCrossFade(500))
//            .into(view)
    }

    @BindingAdapter(value = ["circleImageUrl"])
    @JvmStatic
    fun circleImageUrl(view: ImageView, url: String) {
        Glide.with(view.context.applicationContext)
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(view)
    }


    @BindingAdapter(value = ["afterTextChanged"])
    @JvmStatic
    fun EditText.afterTextChanged(action: (String) -> Unit) {
        addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                action(s.toString())
            }
        })
    }

    @BindingAdapter(value = ["noRepeatClick"])
    @JvmStatic
    fun setOnClick(view: View, clickListener: () -> Unit) {
        val mHits = LongArray(2)
        view.setOnClickListener {
            System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)
            mHits[mHits.size - 1] = SystemClock.uptimeMillis()
            if (mHits[0] < SystemClock.uptimeMillis() - 500) {
                clickListener.invoke()
            }
        }
    }

}